package com.example.xunua_today_in_history;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xunua_today_in_history.bean.HistoryDescBean;
import com.example.xunua_today_in_history.utils.ContentURL;
import com.example.xunua_today_in_history.utils.OkHttpUtil;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

public class HistoryDescActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView img_back,img_send,img_pic;
    private TextView tv_title,tv_content;
    private String hisId;
    private HistoryDescBean.ResultBean resultBean;
    private OkHttpUtil util=new OkHttpUtil();
    
    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what==-1){
                Toast.makeText(HistoryDescActivity.this, "网络开小差了", Toast.LENGTH_SHORT).show();
            } else if (msg.what == 0) {
                tv_title.setText(resultBean.getTitle());
                tv_content.setText(resultBean.getContent());
                String pic = resultBean.getPic();
                if (pic.isEmpty()) {
                    img_pic.setVisibility(View.GONE);
                }else {
                    img_pic.setVisibility(View.VISIBLE);
                    Picasso.with(HistoryDescActivity.this).load(pic).into(img_pic);
                }
            }
        }
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history_desc);
        initView();
        Intent intent = getIntent();
        hisId = intent.getStringExtra("hisId");
        final String historyDescURL = ContentURL.getHistoryDescURL("1.0", hisId);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String s = util.connect_get(historyDescURL);
                    HistoryDescBean historyDescBean = new Gson().fromJson(s, HistoryDescBean.class);
                    resultBean = historyDescBean.getResult().get(0);
                    Message message = new Message();
                    message.what=0;
                    message.obj=resultBean;
                    handler.sendMessage(message);
//                    不能在子线程中更新主页面
//                    tv_title.setText(resultBean.getTitle());
//                    tv_content.setText(resultBean.getContent());
//                    String pic = resultBean.getPic();
//                    if (pic.isEmpty()) {
//                        img_pic.setVisibility(View.GONE);
//                    }else {
//                        img_pic.setVisibility(View.VISIBLE);
//                        Picasso.with(HistoryDescActivity.this).load(pic).into(img_pic);
//                    }
                }catch (Exception e){
                    handler.sendEmptyMessage(-1);
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initView() {
        img_back=findViewById(R.id.desc_img_back);
        img_back.setOnClickListener(this);
        img_send=findViewById(R.id.desc_img_send);
        img_send.setOnClickListener(this);
        img_pic=findViewById(R.id.desc_img_pic);
        tv_title=findViewById(R.id.desc_tv_title);
        tv_content=findViewById(R.id.desc_tv_content);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.desc_img_back:
                finish();
                break;
            case R.id.desc_img_send:
                String text = "我发现一款好用的软件 历史上的今天，一起来探索这个APP吧！";
                if (resultBean!=null) {
                    text = "想要了解"+resultBean.getDes()+"详情么？快来下载 历史上的今天 APP吧！";
                }
                Intent textIntent = new Intent(Intent.ACTION_SEND);//隐式意图 打开分享
                textIntent.setType("text/plain");
                textIntent.putExtra(Intent.EXTRA_TEXT, text);
                startActivity(Intent.createChooser(textIntent, "历史上的今天"));
                break;
        }
    }
}
